package cc.telecomdigital.tdstock.activity.groups.more;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.ActivityGroup_Framework;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Http.H_CODE;
import cc.telecomdigital.tdstock.Http.HttpSuperClassStockBaseActivity;
import cc.telecomdigital.tdstock.Http.Services.HttpAppInfo;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.H_UserClient_HttpRequest_Result;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;
import cc.telecomdigital.tdstock.activity.groups.StockGroup;
import cc.telecomdigital.tdstock.view.EllipsizingTextView;

/**
 * 財星
 * 
 * @author ouyangzhihui
 */
public class StockStarIndexActivity extends HttpSuperClassStockBaseActivity implements OnClickListener
{
	final static private String	TAG	= "StockStarIndexActivity";

	@Override
	protected ActivityGroupController GetActivityGroupController()
	{
		return MoreGroup.GetActivityGroupHostControl();
	}

	static public Class<? extends ActivityGroup_Framework>	iFromClass = null;
	static public boolean iContentPageBack = false;
	final private String[]	iTypeName	= { "01", "02", "03", "04", "05", "06" };
	private EllipsizingTextView[]		txtTitle1	= new EllipsizingTextView[6];
	private Button 			btnReturn;

	// private TextView[] txtTitle2 = new TextView[iTypeName.length];

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_stockstar_index);
		
		btnReturn = (Button) findViewById(R.id.more_returnbtn);
		btnReturn.setOnClickListener(this);
		findViewById(R.id.stockstar_01).setOnClickListener(this);
		findViewById(R.id.stockstar_02).setOnClickListener(this);
		findViewById(R.id.stockstar_03).setOnClickListener(this);
		findViewById(R.id.stockstar_04).setOnClickListener(this);
		findViewById(R.id.stockstar_05).setOnClickListener(this);
		findViewById(R.id.stockstar_06).setOnClickListener(this);

		findViewById(R.id.txtName_01).setOnClickListener(this);
		findViewById(R.id.txtName_02).setOnClickListener(this);
		findViewById(R.id.txtName_03).setOnClickListener(this);
		findViewById(R.id.txtName_04).setOnClickListener(this);
		findViewById(R.id.txtName_05).setOnClickListener(this);
		findViewById(R.id.txtName_06).setOnClickListener(this);
		findViewById(R.id.txtMore_01).setOnClickListener(this);
		findViewById(R.id.txtMore_02).setOnClickListener(this);
		findViewById(R.id.txtMore_03).setOnClickListener(this);
		findViewById(R.id.txtMore_04).setOnClickListener(this);
		findViewById(R.id.txtMore_05).setOnClickListener(this);
		findViewById(R.id.txtMore_06).setOnClickListener(this);
		
		findViewById(R.id.container_01).setOnClickListener(this);
		findViewById(R.id.container_02).setOnClickListener(this);
		findViewById(R.id.container_03).setOnClickListener(this);
		findViewById(R.id.container_04).setOnClickListener(this);
		findViewById(R.id.container_05).setOnClickListener(this);
		findViewById(R.id.container_06).setOnClickListener(this);

		txtTitle1[0] = (EllipsizingTextView) findViewById(R.id.txtTitle1_01);
		txtTitle1[1] = (EllipsizingTextView) findViewById(R.id.txtTitle1_02);
		txtTitle1[2] = (EllipsizingTextView) findViewById(R.id.txtTitle1_03);
		txtTitle1[3] = (EllipsizingTextView) findViewById(R.id.txtTitle1_04);
		txtTitle1[4] = (EllipsizingTextView) findViewById(R.id.txtTitle1_05);
		txtTitle1[5] = (EllipsizingTextView) findViewById(R.id.txtTitle1_06);

//		for (int i = 0; i < txtTitle1.length; i++)
//		{
//			txtTitle1[i].setMaxLines(2);
//		}

		// txtTitle2[0] = (TextView) findViewById(R.id.txtTitle2_01);
		// txtTitle2[1] = (TextView) findViewById(R.id.txtTitle2_02);
		// txtTitle2[2] = (TextView) findViewById(R.id.txtTitle2_03);
		// txtTitle2[3] = (TextView) findViewById(R.id.txtTitle2_04);
		// txtTitle2[4] = (TextView) findViewById(R.id.txtTitle2_05);
		// txtTitle2[5] = (TextView) findViewById(R.id.txtTitle2_06);

		// for(int i = 0; i < txtTitle1.length; i++)
		// {
		// txtTitle1[i].setMovementMethod(LinkMovementMethod.getInstance());
		// txtTitle2[i].setMovementMethod(LinkMovementMethod.getInstance());
		// }
	}

	@Override
	protected void onResume()
	{
		String userName = ((ITDLApplication) getApplicationContext()).getUserName("");
		CommonDefn.ModeType iModeType = ((ITDLApplication) getApplicationContext()).getModeType();
		if (CommonDefn.ModeType.DELARY == iModeType && CommonDefn.ACCOUNT_USER_NAME_TRIAL_DELAY.equals(userName))
		{
			findViewById(R.id.stockstar_container).setVisibility(View.GONE);
			findViewById(R.id.delay_content_tips).setVisibility(View.VISIBLE);
			
			super.SetExectuHttpRequestOnResume(false);
		}
		else
		{
			findViewById(R.id.stockstar_container).setVisibility(View.VISIBLE);
			findViewById(R.id.delay_content_tips).setVisibility(View.GONE);
			
			super.SetExectuHttpRequestOnResume(true);
		}
		super.onResume();
		
		if(iFromClass != null && iFromClass != MoreActivity.class)
		{
			btnReturn.setText(R.string.stock_btn_return_back);
		}
		else
		{
			btnReturn.setText(R.string.more);
		}
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.more_returnbtn:
			{
				if(iFromClass != null && iFromClass != MoreActivity.class)
				{
					SwitchForwardActivity(MoreActivity.class);
					SwitchTabHostActivity(0, StockGroup.GetActivityGroupHostControl(), iFromClass);
				}
				else
				{
					SwitchForwardActivity(MoreActivity.class);
				}
			}
				break;
			case R.id.stockstar_01:
			case R.id.stockstar_02:
			case R.id.stockstar_03:
			case R.id.stockstar_04:
			case R.id.stockstar_05:
			case R.id.stockstar_06:
			case R.id.txtName_01:
			case R.id.txtName_02:
			case R.id.txtName_03:
			case R.id.txtName_04:
			case R.id.txtName_05:
			case R.id.txtName_06:
			case R.id.txtMore_01:
			case R.id.txtMore_02:
			case R.id.txtMore_03:
			case R.id.txtMore_04:
			case R.id.txtMore_05:
			case R.id.txtMore_06:
			{
				TDStockLog.d(TAG, "TagKey: " + v.getTag().toString());

				StockStarListActivity.iTypeName = v.getTag().toString();
				SwitchForwardActivity(StockStarListActivity.class);
			}
				break;
//			case R.id.txtTitle1_01:
//			case R.id.txtTitle1_02:
//			case R.id.txtTitle1_03:
//			case R.id.txtTitle1_04:
//			case R.id.txtTitle1_05:
//			case R.id.txtTitle1_06:
			case R.id.container_01:
			case R.id.container_02:
			case R.id.container_03:
			case R.id.container_04:
			case R.id.container_05:
			case R.id.container_06:
			{
				try
				{
					int index = Integer.parseInt(v.getTag().toString());
					String content = contentList.get(index);//txtTitle1[index].getText().toString();
					if(content != null && !"".equals(content.trim()) && !getResources().getString(R.string.more_stockstar_notData).equals(txtTitle1[index].getText().toString()))
					{
						StockStarContentActivity.iPosition = 1;
						StockStarContentActivity.iTypeName = iTypeName[index];
						StockStarContentActivity.iContent = content;
						StockStarContentActivity.iFromClass = StockStarIndexActivity.class;
						SwitchForwardActivity(StockStarContentActivity.class);
					}
				}
				catch (Exception e)
				{
					TDStockLog.d(TAG, "not forward to content");
				}
			}
				break;
		}
	}

	@Override
	protected H_CODE GetDataFromHttpServer()
	{
		if(iContentPageBack)
		{
			iContentPageBack = false;
			return null;
		}
		
		return HttpAppInfo.SendStockStarIndexRequest(actContext, this);
	}

	private List<String> contentList = new ArrayList<String>();
//	private Calendar 	 calendar 	 = Calendar.getInstance();
	
	@Override
	protected void H_UserClient_HttpRequest_Response_Sub(H_UserClient_HttpRequest_Result iResult)
	{
		if (!iResult.MStatus.equals("0"))
		{
			ShowMessageDialog(iResult.MError + "(" + iResult.MStatus + ")");
			return;
		}
		
		for (int i = 0; i < iResult.contentStores.size(); i++)
		{
			try
			{
				String tmp = iResult.contentStores.get(i)[0];
				
				TDStockLog.d(TAG, "Content:" + tmp);
				
				contentList.add(tmp);
				
				txtTitle1[i].setText(roundDownDateTime(tmp));
			}
			catch(Exception e)
			{
				TDStockLog.w(TAG, "Exception:" + e.toString());
			}
		}
	}
	
	private String roundDownDateTime(String tmp)
	{
		if(tmp != null && tmp.length() >= 12)
		{
			try
			{
				Integer.parseInt(tmp.substring(0, 2));
				tmp = tmp.substring(12);
			}
			catch(Exception e)
			{
				return tmp;
			}
//			try
//			{
//				String date = tmp.substring(0, 2); 
//				String moth = tmp.substring(3, 5);
//				
//				if(Integer.parseInt(date) == calendar.get(Calendar.DATE) && Integer.parseInt(moth) == (1 + calendar.get(Calendar.MONTH)))
//				{
//					tmp = tmp.substring(12);
//					return tmp;
//				}
//			}
//			catch(Exception e)
//			{
//				return getResources().getString(R.string.more_stockstar_notData);
//			}
		}
//		return getResources().getString(R.string.more_stockstar_notData);
		return tmp;
	}

}
